﻿
namespace FastFood.Business.Entities
{
    using System;
    using System.Configuration;
    using System.Xml;

    public class DatosGenerales
    {
        public static Personal PersonalActual { get; set; }

        public static Usuarios UsuarioActual { get; set;}

        public static String RazonSocial 
        { 
            get { return GetValue("RazonSocial"); }
            set { UpdateKey("RazonSocial", value); }
        }

        public static String Rubro 
        {
            get { return GetValue("Rubro"); }
            set { UpdateKey("Rubro", value); }
        }

        public static String RUC 
        {
            get { return GetValue("RUC"); }
            set { UpdateKey("RUC", value); }
        }

        public static String IGV
        {
            get { return GetValue("IGV"); }
            set { UpdateKey("IGV", value); }
        }

        public static String Servicios
        {
            get { return GetValue("Servicios"); }
            set { UpdateKey("Servicios", value); }
        }

        public static String Direccion
        {
            get { return GetValue("Direccion"); }
            set { UpdateKey("Direccion", value); }
        }

        public static String Region
        {
            get { return GetValue("Region"); }
            set { UpdateKey("Region", value); }
        }

        public static String Pais
        {
            get { return GetValue("Pais"); }
            set { UpdateKey("Pais", value); }
        }

        public static String Impresora
        {
            get { return GetValue("Impresora"); }
            set { UpdateKey("Impresora", value); }
        }

        public static String LogoImpresion
        {
            get { return GetValue("LogoImpresion"); }
            set { UpdateKey("LogoImpresion", value); }
        }

        public static string GetValue(string strKey)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
            XmlNode appSettingsNode = xmlDoc.SelectSingleNode("configuration/appSettings");
            foreach (XmlNode node in appSettingsNode.ChildNodes)
            {
                if (node.Attributes["key"].Value == strKey)
                {
                    return node.Attributes["value"].Value;
                }
            }
            return string.Empty;
        }

        public static void AddKey(string strKey, string strValue)
        {
            XmlDocument xmlDoc = new XmlDocument();
            //xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "..\\..\\App.config");
            xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

            XmlNode appSettingsNode = xmlDoc.SelectSingleNode("configuration/appSettings");
            try
            {
                if (KeyExists(strKey, xmlDoc)) throw new ArgumentException("Key name: <" + strKey + "> ya existe en la configuración.");
                XmlNode newChild = appSettingsNode.FirstChild.Clone();
                newChild.Attributes["key"].Value = strKey;
                newChild.Attributes["value"].Value = strValue;
                appSettingsNode.AppendChild(newChild);

                //xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "..\\..\\App.config");
                xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public static void UpdateKey(string strKey, string newValue)
        {
            XmlDocument xmlDoc = new XmlDocument();
            //xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "..\\..\\App.config");
            xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

            if (!KeyExists(strKey, xmlDoc)) throw new ArgumentNullException("Key", "<" + strKey + "> no existe en la configuración. Ha fallado la actualización.");
            XmlNode appSettingsNode = xmlDoc.SelectSingleNode("configuration/appSettings");

            foreach (XmlNode childNode in appSettingsNode)
            {
                if (childNode.Attributes["key"].Value == strKey)
                    childNode.Attributes["value"].Value = newValue;
            }
            //xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "..\\..\\App.config");
            xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        }

        public static void DeleteKey(string strKey)
        {
            XmlDocument xmlDoc = new XmlDocument();
            //xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "..\\..\\App.config");
            xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

            if (!KeyExists(strKey, xmlDoc)) throw new ArgumentNullException("Key", "<" + strKey + "> no existe en la configuración. Ha fallado la actualización.");
            XmlNode appSettingsNode = xmlDoc.SelectSingleNode("configuration/appSettings");

            foreach (XmlNode childNode in appSettingsNode)
            {
                if (childNode.Attributes["key"].Value == strKey)
                    appSettingsNode.RemoveChild(childNode);
            }
            //xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "..\\..\\App.config");
            xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        }

        public static bool KeyExists(string strKey, XmlDocument xmlDoc)
        {
            XmlNode appSettingsNode = xmlDoc.SelectSingleNode("configuration/appSettings");

            foreach (XmlNode childNode in appSettingsNode)
            {
                if (childNode.Attributes["key"].Value == strKey)
                    return true;
            }
            return false;
        }
    }
}
